package com.example.jay10_8;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
    private List<String> mList;
    private Context mContext;

    public HomeAdapter(Context mContext, List<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.tab01_recycle_item,parent,false);
        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.nametv.setText(mList.get(position));
        final String content = mList.get(position);
        String stickyData = mList.get(position);

        holder.nametv.setText(stickyData);

        if (position == 0) {
            holder.nametv.setVisibility(View.VISIBLE);
            holder.nametv.setText(stickyData);
            holder.itemView.setTag(1);
        } else {
            if (!TextUtils.equals(stickyData, mList.get(position - 1))) {
                holder.nametv.setVisibility(View.VISIBLE);
                holder.nametv.setText(stickyData);
                holder.itemView.setTag(2);
            } else {
                holder.nametv.setVisibility(View.GONE);
                holder.itemView.setTag(3);
            }
        }
        holder.itemView.setContentDescription(stickyData);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "你选中的是: " + content, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView nametv;


        public MyViewHolder(View itemView) {
            super(itemView);
            nametv = itemView.findViewById(R.id.tab01_header_view);

        }
    }

}

